<?php
declare (strict_types = 1);

namespace app\admin\middleware;

use app\model\AdminModel;

class Auth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $header = $request->header();

        $token = !empty($header) && isset($header["token"]) ? $header["token"] : "";


        if(!isset($token) || empty($token)){
            return json(['code' => -2, 'msg' => '请先登录']);
        }
        $model = new AdminModel();
        $user = $model->getCache($token);
        if(!$user){
            return json(['code' => -2, 'msg' => '登录已过期']);
        }

        $model->setCache(json_decode($user,true));

        return $next($request);
    }
}
